package com.tourism.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tourism.admin.common.Result;
import com.tourism.admin.model.entity.Permission;
import com.tourism.admin.mapper.PermissionMapper;
import com.tourism.admin.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    @PostMapping("/save")
    public Result<?> save(@RequestBody Permission Permission) {
        permissionService.save(Permission);
        return Result.success();
    }

    @PutMapping("/update")
    public Result<?> update(@RequestBody Permission Permission) {
        permissionService.updateById(Permission);
        return Result.success();
    }

    @DeleteMapping("delete/{id}")
    public Result<?> update(@PathVariable Long id) {
        permissionService.removeById(id);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(permissionService.getById(id));
    }

    @GetMapping("query/all")
    public Result<?> all() {
        return Result.success(permissionService.list(null));
    }

    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Permission> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Permission::getName, search);
        }
        Page<Permission> PermissionPage = permissionService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(PermissionPage);
    }
}
